package com.lgkj.ui.shop;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.GoodsModelAdapter;
import com.lgkj.bean.meitao.GoodsBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.ui.main.CarFragment;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.BottomPopupWindowView;
import com.lgkj.view.ResizableImageView;
import com.meiqia.meiqiasdk.imageloader.MQImage;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/21 0021.
 */

public class ShopDetailsActivivty extends BaseActivity {
    String goodsId;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_old_price)
    TextView tvOldPrice;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_kuai_price)
    TextView tvKuaiPrice;
    @BindView(R.id.tv_shop_title)
    TextView tvShopTitle;
    @BindView(R.id.tv_shop_desc)
    TextView tvShopDesc;
    @BindView(R.id.tv_shop_sale)
    TextView tvShopSale;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.tv_comment_all)
    TextView tvCommentAll;
    @BindView(R.id.ll_shop_desc)
    LinearLayout llShopDesc;
    @BindView(R.id.iv_scro_top)
    ImageView ivScroTop;
    @BindView(R.id.scrol)
    ScrollView scrol;
    BottomPopupWindowView bottomPopup;
    private GoodsBean goodsBean;
    private String goods_model_id = "1";
    public static BigDecimal kuaidi;
    private String imageUrl = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_mei_shop_details;
    }

    @Override
    public void initView() {
        showDiaLog("正在获取数据");
        goodsId = getIntent().getStringExtra("goodsId");
        if (goodsId == null) {
            dismissDiaLog();
            toastLong("当前商品已下架，请重新选择", 0);
            finish();
        }

    }

    @Override
    public void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("goods_id", goodsId);
        if (goodsId == null) {
            return;
        }
//        map.put("goods_id", goodsId);
        HttpUtils.postHttp(map, CommonConstant.goodsInfo, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    goodsBean = mGson.fromJson(String.valueOf(jsonObject), GoodsBean.class);
                    loadData(0);
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void loadData(int postion) {
        if (goodsBean == null && goodsBean.getData() == null) return;
        GoodsBean.DataBean dataBean = goodsBean.getData();
        if (dataBean.getGoodsInfo() != null && dataBean.getGoodsInfo().size() > 0) {
            tvShopSale.setText("销量  " + dataBean.getSales());
            tvOldPrice.setText("￥" + dataBean.getGoodsInfo().get(postion).getOld_price());
            tvPrice.setText("￥会员价 " + dataBean.getGoodsInfo().get(postion).getPrice() + "元");
            goods_model_id = dataBean.getGoodsInfo().get(postion).getId();
        }
        tvCommentAll.setText("商品评价( " + dataBean.getReg_level() + " )");
        kuaidi = new BigDecimal(dataBean.getPostage());
        tvKuaiPrice.setText("快递 " + dataBean.getPostage() + "元");
        tvShopTitle.setText(dataBean.getGoods_name());
        tvShopDesc.setText(dataBean.getGoods_describe());
        imageUrl = dataBean.getPhotos().get(0);
        addImage();
        CommonUtils.LoadImage(ShopDetailsActivivty.this, ivImg, imageUrl, R.mipmap.defult_banner);
    }

    private void addImage() {
        if (goodsBean.getData().getGoods_content() == null)
            return;
        for (int i = 0; i < goodsBean.getData().getGoods_content().size(); i++) {
            ImageView imageView = new ResizableImageView(ShopDetailsActivivty.this);
//            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            String url = goodsBean.getData().getGoods_content().get(i).getUrl();
            CommonUtils.LoadImage(ShopDetailsActivivty.this, imageView, url, 0);
            llShopDesc.addView(imageView);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_comment_all, R.id.cb_collection, R.id.iv_gocar, R.id.iv_gobuy, R.id.iv_scro_top, R.id.tv_kefu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_comment_all:
                Log.i(TAG, "onClick: " + imageUrl);
                Intent intent = new Intent(ShopDetailsActivivty.this, CommentListActivity.class);
//                if (!imageUrl.equals("")) {
//                    intent.putExtra("image_url", imageUrl);
                startsActivity(intent);
//                }
                break;
            case R.id.cb_collection:
                break;
            case R.id.iv_gocar:
                showPopup(true);
                break;
            case R.id.iv_gobuy:
                showPopup(false);
                break;
            case R.id.iv_scro_top:
                scrol.smoothScrollTo(0, 0);
                break;
            case R.id.tv_kefu:
                showkefu();
                break;
        }
    }

    private void showkefu() {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("客服")//标题
                .setMessage("选择服务方式")//内容
                .setPositiveButton("电话", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent1 = new Intent(Intent.ACTION_CALL);
                        Uri data = Uri.parse("tel:0816-298-8116");
                        intent1.setData(data);
                        startsActivity(intent1);
                    }
                })
                .setNeutralButton("客服", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MQImage.setImageLoader(new com.lgkj.utils.MQGlideImageLoader());
                        Intent serviceIntent = new MQIntentBuilder(ShopDetailsActivivty.this).build();
                        startActivity(serviceIntent);
                    }
                })
                .create();
        alertDialog.show();
    }

    private void showPopup(boolean iscar) {

        CarFragment.isResu = true;
        bottomPopup = new BottomPopupWindowView(ShopDetailsActivivty.this);
        bottomPopup.setIsCar(iscar);
        if (goodsBean.getData() == null) {
            return;
        }
        bottomPopup.setData(goodsBean.getData().getGoodsInfo());
        bottomPopup.showAtLocation(ShopDetailsActivivty.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        bottomPopup.setadapter(new GoodsModelAdapter.BackCallClick() {
            @Override
            public void callClick(int position) {
                bottomPopup.initData(position);
            }
        });
    }

    private View.OnClickListener popupClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_colse:
                    bottomPopup.dismiss();
                    break;
                case R.id.btn_save:
                    bottomPopup.dismiss();
                    break;
            }
        }
    };

    /**
     * 加入购物车
     */
    private void addCar() {

    }
}
